//
//  DHSubscription_FollowView.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/6/8.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHSubscription_FollowView.h"
#import "DHHomeHotBrandsCell.h"
#import "DHS2_SearchRecommendBrandsCell.h"
#import "DHS2_SearchRecommendBrandsItemCell.h"

@interface DHSubscription_FollowView()<UITableViewDelegate, UITableViewDataSource>

@property (strong, nonatomic) NSArray *designersArr;
@property (strong, nonatomic) NSArray *brandArr;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation DHSubscription_FollowView

+ (DHSubscription_FollowView *)shareDHSubscription_FollowView{
    DHSubscription_FollowView *view = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([DHSubscription_FollowView class]) owner:self options:nil] objectAtIndex:0];
    return view;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"DHS2_SearchRecommendBrandsCell" bundle:nil] forCellReuseIdentifier:kDHS2_SearchRecommendBrandsCell];
    [self.tableView registerNib:[UINib nibWithNibName:@"DHS2_SearchRecommendBrandsItemCell" bundle:nil] forCellReuseIdentifier:kDHS2_SearchRecommendBrandsItemCell];
    [self.tableView registerNib:[UINib nibWithNibName:@"DHHomeHotBrandsCell" bundle:nil] forCellReuseIdentifier:kDHHomeHotBrandsCell];
}

- (NSArray *)designersArr{
    if (!_designersArr) {
        _designersArr = [NSArray array];
    }
    return _designersArr;
}

- (NSArray *)brandArr{
    if (!_brandArr) {
        _brandArr = [NSArray array];
    }
    return _brandArr;
}

- (void)loadData{
    //推荐设计师
    [[DHNetAPIClient shareDHNetAPIClient] request_brands_recommend_designersWithParams:@{} andBlock:^(id data, NSError *error) {
        self.designersArr = [data objectForKey:@"brands"];
    }];
    
    [[DHNetAPIClient shareDHNetAPIClient] request_brands_recommendsWithParams:@{} andBlock:^(id data, NSError *error) {
        self.brandArr = [data objectForKey:@"recommends"];
        [self.tableView reloadData];
    }];
}

#pragma mark - tableview
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2 + self.brandArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        CGFloat w = (kScreenWidth - 32) / 3;
        CGFloat h = w * 0.51;
        if (self.designersArr.count % 3 == 0) {
            return 87 + (self.designersArr.count / 3) * h;
        }else{
            return 87 + (self.designersArr.count / 3 + 1) * h;
        }
    }else if (indexPath.row == 1){
        return 50;
    }else{
        NSDictionary *dic = [self.brandArr objectAtIndex:indexPath.row - 2];;
        NSArray *arr = [dic objectForKey:@"products"];
        if (arr.count % 2 ==0) {
            return 78 + (arr.count / 2) * 280;
        }else{
            return 78 + (arr.count / 2 + 1) * 280;
        }
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        DHHomeHotBrandsCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHHomeHotBrandsCell forIndexPath:indexPath];
        [cell updateCellWithArray:self.designersArr];
        cell.nameLabel.text = @"推荐设计师";
        cell.moreView.hidden = YES;
        return cell;
    }else if (indexPath.row == 1){
        DHS2_SearchRecommendBrandsCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHS2_SearchRecommendBrandsCell forIndexPath:indexPath];
        return cell;
    }else{
        DHS2_SearchRecommendBrandsItemCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHS2_SearchRecommendBrandsItemCell forIndexPath:indexPath];
        NSDictionary *dic = [self.brandArr objectAtIndex:indexPath.row - 2];
        [cell updateCell:dic];
        return cell;
    }
}

@end
